﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dropthings.Util;
using Dropthings.Business.Facade;
using Dropthings.Web.Util;
using System.Text;
using Dropthings.Model;

public partial class Admin_MemberManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            ControlDataBind();
    }

    private void ControlDataBind()
    {
        var facade = Services.Get<Facade>();
        {
            lvItems.DataSource = facade.GetPagedMember();
            lvItems.DataBind();
            Multiview.ActiveViewIndex = 0;
        }
    }
    
    protected void ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (string.Equals(e.CommandName, "DeleteItem"))
        {
            string userName = e.CommandArgument.ToString();
            if (!string.IsNullOrEmpty(userName))
            {
                var facade = Services.Get<Facade>();
                {
                    facade.DeleteUser(userName);
                    ControlDataBind();
                    WebUtil.ShowMessage(lblMessage, "Member successfully deleted.", false);
                }
            }
        }
        else if (string.Equals(e.CommandName, "EditUserRole"))
        {
            var facade = Services.Get<Facade>();
            {
                cblUserRole.DataSource= facade.GetAllRole();
                cblUserRole.DataTextField = "RoleName";
                cblUserRole.DataValueField = "RoleId";
                cblUserRole.DataBind();
                hfUserName.Value = e.CommandArgument.ToString();
                List<Dropthings.Data.AspNetRole> roleLists = facade.GetRolesOfUser(new Guid(e.CommandArgument.ToString()));
                foreach (ListItem li in cblUserRole.Items)
                {
                    foreach (Dropthings.Data.AspNetRole role in roleLists)
                    {
                        if (string.Equals(li.Value, role.RoleId.ToString(), StringComparison.OrdinalIgnoreCase))
                        {
                            li.Selected = true;
                        }
                    }
                }
                Multiview.ActiveViewIndex = 1;
            }
        }
    }
    
    protected void CancelClicked(object sender, EventArgs e)
    {
        ControlDataBind();
    }

    protected void SaveClicked(object sender, EventArgs e)
    {
        var facade = Services.Get<Facade>();
        {
            string userName = facade.GetUserNamedFromUserGuid(new Guid(hfUserName.Value));
            StringBuilder sb = new StringBuilder();
            foreach (ListItem li in cblUserRole.Items)
            {
                if (li.Selected)
                    sb.AppendFormat("{0};", li.Text);
            }
            if (sb.Length < 1)
            {
                WebUtil.ShowMessage(lblMessage, "请选择一个角色.", true);
                return;
            }
            facade.SetUserRoles(userName, sb.ToString().Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries));
            ControlDataBind();
            WebUtil.ShowMessage(lblMessage, "添加角色成功", false);
        }
    }

    protected void AddNewUserClicked(object sender, EventArgs e)
    {
        this.Multiview.ActiveViewIndex = 2;
    }

    protected void CancelUserClicked(object sender, EventArgs e)
    {
        ControlDataBind();
    }

    protected void SaveUserClicked(object sender, EventArgs e)
    {
        RegisterUserResponse registerUserResponse = null;
        var facade = Services.Get<Facade>();
        {
            registerUserResponse = facade.RegisterUser(UserNameTxt.Text, PasswordTxt.Text, UserNameTxt.Text, false);
        }
        ControlDataBind();
        if (registerUserResponse!=null)
            WebUtil.ShowMessage(lblMessage, "添加用户成功", false);
        else
            WebUtil.ShowMessage(lblMessage, "添加用户失败", false);
    }
    

}